<?php
namespace Home\Action;
use Common\Model\userModel;
use Common\Model\usercashModel;
class WechatAction extends BaseAction {
private $accessKey = '';
public function _initialize()
    {
        parent::_initialize();
        $this->accessKey = trim(C('yh_gongju'));
$this->openid=I('openid','','trim');
$this->uid=F('wechat_'.$this->openid);
$this->mdomain=str_replace('/index.php/m','',trim(C('yh_headerm_html')));
if($this->openid && false === $this->uid){
$mod = new userModel();
$this->uid=$mod->where(array('openid'=>$this->openid))->getField('id');

if(!$this->uid){
	$json=array(
	'state'=>2
	);
	exit(json_encode($json));
}

F('wechat_'.$this->openid,$this->uid);
}

 }
 
public function haibao(){
$this->check_key();	
$map=array(
'openid'=>$this->openid
);
$Mod = new userModel();
$AgentId=$Mod->field('id,invocode,tbname')->where($map)->find();
if($AgentId && $AgentId['tbname']==1){
$Json=array(
'state'=>1,
'invocode'=>$AgentId['invocode'],
'isopen'=>C('yh_invocode'),
'msg'=>$AgentId['id']
);
}elseif($AgentId){

$Json=array(
'state'=>4,
'msg'=>'您还不是代理，无法生成海报
👉<a href="'.$this->mdomain.'/index.php?m=m&c=help&a=agent">点我了解代理政策</a>'
);	

}else{
$Json=array(
'state'=>2
);	
}

exit(json_encode($Json));
	
}
 
public function balance(){
$this->check_key();	
$map=array(
'openid'=>$this->openid
);
$Mod = new userModel();
$result=$Mod->field('money,frozen,score')->where($map)->find();
if($result){
$Json=array(
'state'=>1,
'msg'=>'◇◇◇账户信息◇◇◇
当前积分：'.$result['score'].'
账户余额：'.$result['money'].'元
冻结资金：'.$result['frozen'].'元
当余额达到'.C('yh_Quota').'元就可以提现啦！
👉<a href="'.$this->mdomain.'/index.php?m=m&c=user&a=tixian">点我申请提现</a>'
);	


exit(json_encode($Json));
	
}else{
$json=array(
	'state'=>2,
	'msg'=>'没有查询到您的账户信息'
	);
}
exit(json_encode($json));	
	
}


public function checkin(){
$this->check_key();
$Amount=abs(trim(C('yh_item_hit')));
if($Amount){
$Amount=mt_rand(1,$Amount*100)/100;
$mod_cash=new usercashModel();
$today=date('Y-m-d',NOW_TIME);
$checked=S('checkin_'.$this->uid);
if($this->uid && false ===$checked){
S('checkin_'.$this->uid,$this->uid,2);
$ischeck=$mod_cash->where(array('uid'=>$this->uid,'type'=>12))->order('id desc')->getField('create_time');
	if($today==date('Y-m-d',$ischeck)){
	$data=array(
	'msg'=>'今日已签到，请明天再来！

[玫瑰]置顶公众号 找券更方便！[玫瑰]

👉<a href="'.$this->mdomain.'/index.php?m=m&c=help&a=index&id=4">《新手领券指南》</a>',
	'state'=>1
	);
	exit(json_encode($data));
	}

$data=array(
'uid'=>$this->uid,
'money'=>$Amount,
'type'=>12,
'remark'=>'签到送现金',
'create_time'=>NOW_TIME,
'status'=>1
);

$res=$mod_cash->add($data);
	if($res){
		$mod=new userModel();
		$mod->where("id='".$this->uid."'")->setInc('money',$Amount);
		$json=array(
		'msg'=>'签到成功，恭喜获得'.$Amount.'元
--------------------------
[玫瑰]置顶公众号 找券更方便！[玫瑰]

👉<a href="'.$this->mdomain.'/index.php?m=m&c=help&a=index&id=4">《新手领券指南》</a>',
		'state'=>1
		);
		exit(json_encode($json));
	}


}


$data=array(
'msg'=>'请不要重复签到。
--------------------------
[玫瑰]置顶公众号 找券更方便！[玫瑰]

👉<a href="'.$this->mdomain.'/index.php?m=m&c=help&a=index&id=4">《新手领券指南》</a>',
'state'=>1
);

exit(json_encode($data));

	
}	

$data=array(
'msg'=>'还没有开启签到功能！',
'state'=>1
);

exit(json_encode($data));
	
}





protected  function check_key(){
 $json = array(
         'state'=>'no',
          'msg'=>'通行密钥不正确'
         );

$key = I('key', '', 'trim');
if(!$key || $key!=$this->accessKey){
exit(json_encode($json));
}	
}


}